package velhaxo.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import velhaxo.model.Correio;

public class TelaJogo extends JFrame
{

	private JPanel contentPane;
	private JLabel lblJogador1;
	private JLabel lblJogador2;
	private Map<String,ButtonXO> mapaBotoes = new HashMap<String,ButtonXO>();
	private String marcador;
	
	Correio correio;
	private ButtonXO buttonA1;
	private ButtonXO buttonB1;
	private ButtonXO buttonC1;
	private JLabel lblStatus;
	private JLabel lblStatusValor;
	private ButtonXO buttonA2;
	private ButtonXO buttonB2;
	private ButtonXO buttonC2;
	private ButtonXO buttonA3;
	private ButtonXO buttonB3;
	private ButtonXO buttonC3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					TelaJogo frame = new TelaJogo();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	public void pirarLegal()
	{
		// TODO Auto-generated method stub
		synchronized (this)
		{
			System.out.println();
			notify();
			System.out.println();
		}
	}

	/**
	 * Create the frame.
	 */
	public TelaJogo()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JLabel lblNewLabel = new JLabel("Jogador1:");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 0;
		panel.add(lblNewLabel, gbc_lblNewLabel);
		
		lblJogador1 = new JLabel("N\u00E3o conectado");
		GridBagConstraints gbc_lblJogador1 = new GridBagConstraints();
		gbc_lblJogador1.insets = new Insets(0, 0, 5, 5);
		gbc_lblJogador1.gridx = 2;
		gbc_lblJogador1.gridy = 0;
		panel.add(lblJogador1, gbc_lblJogador1);
		
		JLabel lblNewLabel_1 = new JLabel("Jogador2:");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 1;
		gbc_lblNewLabel_1.gridy = 1;
		panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		lblJogador2 = new JLabel("N\u00E3o conectado");
		GridBagConstraints gbc_lblJogador2 = new GridBagConstraints();
		gbc_lblJogador2.insets = new Insets(0, 0, 5, 5);
		gbc_lblJogador2.gridx = 2;
		gbc_lblJogador2.gridy = 1;
		panel.add(lblJogador2, gbc_lblJogador2);
		
		buttonA1 = new ButtonXO(ButtonXO.POSICAO_A1);
		mapaBotoes.put(ButtonXO.POSICAO_A1, buttonA1);
		buttonA1.addActionListener(new ButtonActionListener(this));
		buttonA1.setText(" ");
		GridBagConstraints gbc_buttonA1 = new GridBagConstraints();
		gbc_buttonA1.insets = new Insets(0, 0, 5, 5);
		gbc_buttonA1.gridx = 3;
		gbc_buttonA1.gridy = 3;
		panel.add(buttonA1, gbc_buttonA1);
		
		buttonB1 = new ButtonXO(ButtonXO.POSICAO_B1);
		buttonB1.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_B1, buttonB1);
		buttonB1.setText(" ");
		GridBagConstraints gbc_buttonB1 = new GridBagConstraints();
		gbc_buttonB1.insets = new Insets(0, 0, 5, 5);
		gbc_buttonB1.gridx = 4;
		gbc_buttonB1.gridy = 3;
		panel.add(buttonB1, gbc_buttonB1);
		
		buttonC1 = new ButtonXO(ButtonXO.POSICAO_C1);
		buttonC1.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_C1, buttonC1);
		buttonC1.setText(" ");
		GridBagConstraints gbc_buttonC1 = new GridBagConstraints();
		gbc_buttonC1.insets = new Insets(0, 0, 5, 0);
		gbc_buttonC1.gridx = 5;
		gbc_buttonC1.gridy = 3;
		panel.add(buttonC1, gbc_buttonC1);
		
		buttonA2 = new ButtonXO(ButtonXO.POSICAO_A2);
		buttonA2.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_A2, buttonA2);
		buttonA2.setText(" ");
		GridBagConstraints gbc_buttonA2 = new GridBagConstraints();
		gbc_buttonA2.insets = new Insets(0, 0, 5, 5);
		gbc_buttonA2.gridx = 3;
		gbc_buttonA2.gridy = 4;
		panel.add(buttonA2, gbc_buttonA2);
		
		buttonB2 = new ButtonXO(ButtonXO.POSICAO_B2);
		buttonB2.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_B2, buttonB2);
		buttonB2.setText(" ");
		GridBagConstraints gbc_buttonB2 = new GridBagConstraints();
		gbc_buttonB2.insets = new Insets(0, 0, 5, 5);
		gbc_buttonB2.gridx = 4;
		gbc_buttonB2.gridy = 4;
		panel.add(buttonB2, gbc_buttonB2);
		
		buttonC2 = new ButtonXO(ButtonXO.POSICAO_C2);
		buttonC2.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_C2, buttonC2);
		buttonC2.setText(" ");
		GridBagConstraints gbc_buttonC2 = new GridBagConstraints();
		gbc_buttonC2.insets = new Insets(0, 0, 5, 0);
		gbc_buttonC2.gridx = 5;
		gbc_buttonC2.gridy = 4;
		panel.add(buttonC2, gbc_buttonC2);
		
		buttonA3 = new ButtonXO(ButtonXO.POSICAO_A3);
		buttonA3.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_A3, buttonA3);
		buttonA3.setText(" ");
		GridBagConstraints gbc_buttonA3 = new GridBagConstraints();
		gbc_buttonA3.insets = new Insets(0, 0, 5, 5);
		gbc_buttonA3.gridx = 3;
		gbc_buttonA3.gridy = 5;
		panel.add(buttonA3, gbc_buttonA3);
		
		buttonB3 = new ButtonXO(ButtonXO.POSICAO_B3);
		buttonB3.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_B3, buttonB3);
		buttonB3.setText(" ");
		GridBagConstraints gbc_buttonB3 = new GridBagConstraints();
		gbc_buttonB3.insets = new Insets(0, 0, 5, 5);
		gbc_buttonB3.gridx = 4;
		gbc_buttonB3.gridy = 5;
		panel.add(buttonB3, gbc_buttonB3);
		
		buttonC3 = new ButtonXO(ButtonXO.POSICAO_C3);
		buttonC3.addActionListener(new ButtonActionListener(this));
		mapaBotoes.put(ButtonXO.POSICAO_C3, buttonC3);
		buttonC3.setText(" ");
		GridBagConstraints gbc_buttonC3 = new GridBagConstraints();
		gbc_buttonC3.insets = new Insets(0, 0, 5, 0);
		gbc_buttonC3.gridx = 5;
		gbc_buttonC3.gridy = 5;
		panel.add(buttonC3, gbc_buttonC3);
		
		lblStatus = new JLabel("Status:");
		GridBagConstraints gbc_lblStatus = new GridBagConstraints();
		gbc_lblStatus.insets = new Insets(0, 0, 0, 5);
		gbc_lblStatus.gridx = 1;
		gbc_lblStatus.gridy = 8;
		panel.add(lblStatus, gbc_lblStatus);
		
		lblStatusValor = new JLabel("Aguardando");
		GridBagConstraints gbc_lblStatusValor = new GridBagConstraints();
		gbc_lblStatusValor.anchor = GridBagConstraints.WEST;
		gbc_lblStatusValor.gridwidth = 4;
		gbc_lblStatusValor.gridx = 2;
		gbc_lblStatusValor.gridy = 8;
		panel.add(lblStatusValor, gbc_lblStatusValor);
		
	}

	public JLabel getLblJogador1()
	{
		return lblJogador1;
	}

	public void setLblJogador1(JLabel lblJogador1)
	{
		this.lblJogador1 = lblJogador1;
	}

	public JLabel getLblJogador2()
	{
		return lblJogador2;
	}

	public void setLblJogador2(JLabel lblJogador2)
	{
		this.lblJogador2 = lblJogador2;
	}

	public Correio getCorreio()
	{
		return correio;
	}

	public void setCorreio(Correio correio)
	{
		this.correio = correio;
	}

	public Map<String, ButtonXO> getMapaBotoes()
	{
		return mapaBotoes;
	}

	public void setMapaBotoes(Map<String, ButtonXO> mapaBotoes)
	{
		this.mapaBotoes = mapaBotoes;
	}

	public String getMarcador()
	{
		return marcador;
	}

	public void setMarcador(String marcador)
	{
		this.marcador = marcador;
	}
	
}

class ButtonActionListener implements ActionListener
{
	TelaJogo telaJogoInner;
	public ButtonActionListener(TelaJogo telaJogo)
	{
		this.telaJogoInner = telaJogo;
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
			Object source = arg0.getSource();
			ButtonXO bt = (ButtonXO) source;
			bt.setText(telaJogoInner.getMarcador());
			String posicao = bt.getPosicao();
			telaJogoInner.getCorreio().postarJogada(posicao);
			System.out.println();
	}
	
}
